package org.esk.dablog.web.forms.ride;

import org.esk.dablog.web.elements.CalendarBean;
import org.esk.dablog.web.forms.AddEntryForm;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.model.entries.Ride;
import org.esk.dablog.model.Entry;
import org.esk.dablog.model.Author;
import org.esk.dablog.model.Comment;
import org.esk.dablog.exceptions.ParameterException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Calendar;

/**
 * This class
 * User: esk
 * Date: 08.12.2006
 * Time: 20:26:31
 * $Id:$
 */
public class AddRideForm extends AddEntryForm {


    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors) throws Exception {
        ModelAndView show = super.showForm(request, response, errors);
        Calendar c = Calendar.getInstance();

        Date d = RequestUtils.toDateParameter(request);
        if (d != null) {
            c.setTime(d);
        }
        CalendarBean bean = new CalendarBean();
        bean.setLocale(request.getLocale());
        bean.setShowMonths(true);
        bean.setCurrentDate(c);
        bean.setDayHref(request.getContextPath() + "/addride.form");
        show.addObject("calendar", bean);
        show.addObject("date", d);
        return show;
    }


    protected void processEntry(HttpServletRequest request, Entry object) throws Exception {
        Ride e = (Ride) object;
        Comment c = e.join(e.getAuthor());
        c.setIp(request.getRemoteAddr());
    }

    protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
        return new Ride();
    }
}
